/**
 * Created by walkman on 2017/6/23.
 */
(function () {
  angular.module('WfbPlatformApp').controller('ProfileController',
    ['$rootScope', '$scope', '$http', '$compile', 'http', '$q', '$state', 'md5', 'profileService',
      function ($rootScope, $scope, $http, $compile, http, $q, $state, md5, profileService) {

        $scope.$on('$viewContentLoaded', function () {
          // initialize core components

          $("#ziliaoDl dl dt").click(function () {
            var dlClass = $(this).parents("dl");
            if ($(this).attr('id') == 'identy-dt' && validated_number_today >= 2) {
              return false;
            }
            $("#ziliaoDl").find("dd").hide();
            if (!dlClass.hasClass("curr")) {
              $("#ziliaoDl").find("dl").removeClass("curr");
              dlClass.addClass("curr");
              dlClass.find("dd").slideToggle("slow");
            }
            else {
              dlClass.find("dd").hide();
              dlClass.removeClass("curr");
            }
          });
        });

        $scope.$on('$includeContentLoaded', function () {
        });
        $scope.LoginPwdChgModel = {};
        $scope.PayPwdChgModel = {};
        $scope.PhoneBindModel = {};
        $scope.wait = 60;
        $scope.PacketBindInfo = {};
        $scope.PhoneBindInfo = {};
        $scope.PayPwdSetInfo = {};

        $scope.getPayPwdSetInfo = function () {

          var promise = profileService.getPayPwdSetInfo({});
          promise.then(function success(result) {
            //成功回调
            $scope.PayPwdSetInfo = angular.copy(result.data);
          }, function error(error) {
            //错误回调
            // console.log(JSON.stringify(error));
          });
        };

        //钱包绑定情况
        $scope.getPacketBindInfo = function () {
          var promise = profileService.getPacketBindInfo({});
          promise.then(function success(result) {
            //成功回调
            $scope.PacketBindInfo = angular.copy(result.data);
          }, function error(error) {
            //错误回调
            // console.log(JSON.stringify(error));
          });
        };

        //手机绑定情况
        $scope.getPhoneBindInfo = function () {

          var promise = profileService.getPhoneBindInfo({});
          promise.then(function success(result) {
            //成功回调
            $scope.PhoneBindInfo = angular.copy(result.data);
          }, function error(error) {
            //错误回调
            // console.log(JSON.stringify(error));
          });
        };
        //修改登录密码
        $scope.changeLoginPwd = function () {

          $('body').mLoading('show');
          var new_pwd = $scope.LoginPwdChgModel.newPassword,
            r_new_pwd = $scope.LoginPwdChgModel.newConfirmPassword;
          if (new_pwd !== r_new_pwd) {
            $('body').mLoading('hide');
            // error('两次输入的登录密码不一样', function () {
            //
            // })
            return false;
          }

          var promise = profileService.modifyPassword({
            oldPassword: md5.createHash($scope.LoginPwdChgModel.oldPassword || ''),
            newPassword: md5.createHash($scope.LoginPwdChgModel.newPassword || ''),
            newConfirmPassword: md5.createHash($scope.LoginPwdChgModel.newConfirmPassword || '')
          });

          promise.then(function success(result) {
            //成功回调
            if (result.retCode == '1') {
              $('body').mLoading('hide');
              // success(data.msg, function () {
              //   location.href = "";
              // });
            }
          }, function error(error) {//错误回调
            $('body').mLoading('hide');
            // error(data.msg, function () {
            // });
          });
        };

        //修改或设置交易密码
        $scope.setOrChangePayPwd = function () {

          $('body').mLoading('show');
          var new_pay_pwd = $scope.PayPwdChgModel.newTradePassword,
            r_new_pay_pwd = $scope.PayPwdChgModel.newConfirmTradePassword;
          if (new_pay_pwd !== r_new_pay_pwd) {
            $('body').mLoading('hide');
            // error('两次输入的交易密码不一样', function () {
            // });
            return false;
          }

          var promise;
          if ($scope.PayPwdSetInfo.hasTradePassword) {
            promise = profileService.modifyTradePassword({
              oldTradePassword: md5.createHash($scope.PayPwdChgModel.oldTradePassword || ''),
              tradePassword: md5.createHash($scope.PayPwdChgModel.tradePassword || ''),
              confirmTradePwd: md5.createHash($scope.PayPwdChgModel.confirmTradePwd || '')
            });
          } else {
            promise = profileService.setTradePassword({
              tradePassword: md5.createHash($scope.PayPwdChgModel.tradePassword || ''),
              confirmTradePwd: md5.createHash($scope.PayPwdChgModel.confirmTradePwd || '')
            });
          }
          promise.then(function success(result) {
            //成功回调
            if (result.retCode == '1') {
              $('body').mLoading('hide');
              // success(data.msg, function () {
              //   location.href = "";
              // });
            }
          }, function error(error) {
            //错误回调
            $('body').mLoading('hide');
            // error(data.msg, function () {
            // });
          });
        };

        $scope.bindMobilePhone = function () {

          $('body').mLoading('show');
          var new_pwd = $scope.PhoneBindModel.newPassword,
            r_new_pwd = $scope.PhoneBindModel.newConfirmPassword;
          if (new_pwd !== r_new_pwd) {
            $('body').mLoading('hide');
            // error('两次输入的登录密码不一样', function () {
            //
            // })
            return false;
          }
          var promise = profileService.bindMobilePhone($scope.PhoneBindModel);
          promise.then(function success(result) {
            //成功回调
            if (result.retCode == '1') {
              $('body').mLoading('hide');
              // success(data.msg, function () {
              //   location.href = "";
              // });
            }
          }, function error(error) {//错误回调
            $('body').mLoading('hide');
            // error(data.msg, function () {
            //
            // });
          });
        };

        // $scope.time = function (o, style) {
        //
        //   if (wait == 0) {
        //     if (style == 1) {
        //       $(o).bind('click', getCode);
        //     }
        //     if (style == 2) {
        //       $(o).bind('click', getCode2);
        //     }
        //     $(o).val("获取验证码");
        //     wait = 60;
        //   } else {
        //     if (style == 1) {
        //       $('#getCode').unbind();
        //     }
        //     if (style == 2) {
        //       $('#getCode2').unbind();
        //     }
        //
        //     $(o).val("重新发送(" + wait + ")");
        //     wait--;
        //     setTimeout(function () {
        //         time(o, style)
        //       },
        //       1000)
        //   }
        // }

        // $scope.getCode2 = function () {
        //
        //   $('body').mLoading('show');
        //   $('#getCode2').unbind('click');
        //   $.post("/account/getcode", {}, function (data) {
        //     if (data.code == 1) {
        //       $('body').mLoading('hide');
        //       time($("#getCode2"), 2)
        //       success(data.msg, function () {
        //         $('#getCode2').bind('click', getCode2);
        //       });
        //     } else {
        //       $('body').mLoading('hide');
        //       error(data.msg, function () {
        //         $('#getCode2').bind('click', getCode2);
        //       });
        //     }
        //   });
        // };

        // $scope.getCode = function () {
        //
        //   $('#getCode').unbind('click');
        //   $('body').mLoading('show');
        //   $.post("/account/getcode", {}, function (data) {
        //     if (data.code == 1) {
        //       $('body').mLoading('hide');
        //       time($("#getCode"), 1)
        //       success(data.msg, function () {
        //         $('#getCode').bind('click', getCode);
        //       })
        //     } else {
        //       $('body').mLoading('hide');
        //       error(data.msg, function () {
        //         $('#getCode').bind('click', getCode);
        //       });
        //     }
        //   });
        // };

        $scope.gotobind = function () {
          $state.go("transaction.buy.bind");
        };

        // $(function () {
        //
        //   $("#ziliaoDl dl dt").click(function () {
        //
        //     var dlClass = $(this).parents("dl");
        //     if ($(this).attr('id') == 'identy-dt' && validated_number_today >= 2) {
        //       return false;
        //     }
        //
        //     $("#ziliaoDl").find("dd").hide();
        //     if (!dlClass.hasClass("curr")) {
        //       $("#ziliaoDl").find("dl").removeClass("curr");
        //       dlClass.addClass("curr");
        //       dlClass.find("dd").slideToggle("slow");
        //     }
        //     else {
        //       dlClass.find("dd").hide();
        //       dlClass.removeClass("curr");
        //     }
        //   })
        //
        //   $('#changeLog').bind('click', changeLog);
        //   $('#changePay').bind('click', changePay);
        //
        //   $('#getCode').bind('click', getCode);
        //   $('#getCode2').bind('click', getCode2);
        //
        // })

        $scope.getPayPwdSetInfo();
        $scope.getPacketBindInfo();
        $scope.getPhoneBindInfo();

      }]);
})();


